package com.weilx959.spt.core.component;

import com.weilx959.spt.core.serialize.ValueDeserializer;
import com.weilx959.spt.core.serialize.ValueSerializer;
import com.weilx959.spt.core.utils.Try;
import javafx.scene.Node;

import java.util.Objects;

/**
 * 隐藏表单项控制
 *
 * @param <T> 表单项类型
 * @author weilx
 * @since 2024年6月5日
 */
public class HideFormItemControl<T> extends ParentFormItemControl<T> {
    @Override
    public Try<String> validate() {
        return new Try.Success<>(null);
    }

    @Override
    public boolean isModified() {
        return false;
    }

    @Override
    public void reset() {
        // do nothing
    }

    @Override
    public void submit() {
        // do nothing
    }

    @Override
    public Node getControl() {
        return null;
    }

    // builder
    public static <T> HideFormItemControlBuilder<T> builder() {
        return new HideFormItemControlBuilder<>();
    }

    public static class HideFormItemControlBuilder<T> {
        private String key;
        private T value;
        private ValueSerializer<T> serializer;
        private ValueDeserializer<T> deserializer;

        public HideFormItemControlBuilder<T> key(String key) {
            this.key = key;
            return this;
        }

        public HideFormItemControlBuilder<T> value(T value) {
            this.value = value;
            return this;
        }

        public HideFormItemControlBuilder<T> serializer(ValueSerializer<T> serializer) {
            this.serializer = serializer;
            return this;
        }

        public HideFormItemControlBuilder<T> deserializer(ValueDeserializer<T> deserializer) {
            this.deserializer = deserializer;
            return this;
        }

        public HideFormItemControl<T> build() {
            HideFormItemControl<T> control = new HideFormItemControl<>();
            control.setKey(key);
            control.setValue(value);

            if (Objects.nonNull(serializer)) {
                control.setSerializer(serializer);
            }
            if (Objects.nonNull(deserializer)) {
                control.setDeserializer(deserializer);
            }
            return control;
        }
    }
}
